package com.lovo;


import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Homework {
    public static void main(String[] args) {
        Homework homework = new Homework();
        System.out.println(homework.getDate());

        System.out.println(homework.camelToSnake("camelToSnake"));

        System.out.println(homework.snakeToCamel("camel_to_snake"));

        System.out.println(homework.formatCurrency(1234567890.0345));


    }

    /**
     * 1. 编写一个函数，实现日期格式化处理
     *
     * @return
     */
    public String getDate() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月d日");
        LocalDate localDate = LocalDate.now();
        return localDate.format(formatter);
    }

    /**
     * 2. 编写一个函数，将驼峰命名法(Camel Case)的单词转换为蛇形命名法(Snake Case)的单词。
     *
     * @param s
     * @return
     */
    public String camelToSnake(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c > 'A' && c < 'Z' && i != 0) {
                str += "_" + c;
            } else {
                str += c;
            }
        }
        return str.toLowerCase();
    }

    /**
     * 3. 编写一个函数，将蛇形命名法的单词转换为驼峰命名法的单词。
     *
     * @param s
     * @return
     */
    public String snakeToCamel(String s) {
        String str = "";
        String[] strArr = s.split("_");
        for (int i = 0; i < strArr.length; i++) {
            if (i != 0) {
                char[] tmp = strArr[i].toCharArray();
                tmp[0] -= 32;
                strArr[i] = new String(tmp);
            }
            str += strArr[i];
        }
        return str;
    }

    /**
     * 4. 编写一个函数，将货币数字以千分位的方式表示。
     *
     * @param num
     * @return
     */
    public String formatCurrency(double num) {
        int intNum = (int) num;
        //存储小数部分
        String small = String.valueOf(num - intNum);
        //判断整数部分有几段
        int len = 0;
        while (intNum > 0) {
            intNum /= 1000;
            len++;
        }
        //拆分数字放入数组
        String[] intArr = new String[len];
        intNum = (int) num;
        int i = len - 1;
        while (i >= 0) {
            intArr[i] = String.valueOf(intNum % 1000);
            intNum /= 1000;
            i--;
        }
        //拼接整数部分
        String str = "";
        for (int j = 0; j < intArr.length; j++) {
            if (j < intArr.length - 1) {
                str += intArr[j] + ",";
            } else {
                str += intArr[j];
            }
        }
        //拼接小数部分
        return str += small.substring(1);
    }
}
